import os
from typing import Dict, Any

class Config:
    """基础配置类"""
    
    # Flask配置
    SECRET_KEY = os.getenv('SECRET_KEY', 'your-secret-key-here')
    JWT_SECRET_KEY = os.getenv('JWT_SECRET_KEY', 'jwt-secret-string')
    JWT_TOKEN_LOCATION = ['headers']
    JWT_HEADER_NAME = 'Authorization'
    JWT_HEADER_TYPE = 'Bearer'
    
    # 数据库配置
    SQLALCHEMY_DATABASE_URI = os.getenv(
        'DATABASE_URL', 
        'mysql+pymysql://root:mario@localhost/ai_ssq?charset=utf8mb4'
    )
    SQLALCHEMY_TRACK_MODIFICATIONS = False
    
    # AI服务配置
    DEEPSEEK_API_KEY = 'sk-d8b48b0fcd5e4ee7823d7ce71db41fde'
    DEEPSEEK_API_URL = 'https://api.deepseek.com/v1/chat/completions'
    DEEPSEEK_MODEL = "deepseek-chat"
    
    # 爬虫配置
    CRAWLER_BASE_URL = "https://www.lottery.gov.cn/historykj/history.jspx?_ltype=4"
    CRAWLER_USER_AGENT = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36'
    CRAWLER_TIMEOUT = 10
    CRAWLER_MAX_PAGES = 3
    CRAWLER_TARGET_COUNT = 30
    
    # 日志配置
    LOG_LEVEL = os.getenv('LOG_LEVEL', 'INFO')
    LOG_FILE = 'logs/ai_ssq.log'
    LOG_MAX_BYTES = 10240
    LOG_BACKUP_COUNT = 10
    
    # 分析配置
    MAX_ANALYSIS_PERIODS = 100
    MIN_ANALYSIS_PERIODS = 10
    
    @classmethod
    def get_database_config(cls) -> Dict[str, Any]:
        """获取数据库配置"""
        return {
            'uri': cls.SQLALCHEMY_DATABASE_URI,
            'track_modifications': cls.SQLALCHEMY_TRACK_MODIFICATIONS
        }
    
    @classmethod
    def get_ai_config(cls) -> Dict[str, Any]:
        """获取AI服务配置"""
        return {
            'api_key': cls.DEEPSEEK_API_KEY,
            'api_url': cls.DEEPSEEK_API_URL,
            'model': cls.DEEPSEEK_MODEL
        }
    
    @classmethod
    def get_crawler_config(cls) -> Dict[str, Any]:
        """获取爬虫配置"""
        return {
            'base_url': cls.CRAWLER_BASE_URL,
            'user_agent': cls.CRAWLER_USER_AGENT,
            'timeout': cls.CRAWLER_TIMEOUT,
            'max_pages': cls.CRAWLER_MAX_PAGES,
            'target_count': cls.CRAWLER_TARGET_COUNT
        }
    
    @classmethod
    def get_logging_config(cls) -> Dict[str, Any]:
        """获取日志配置"""
        return {
            'level': cls.LOG_LEVEL,
            'file': cls.LOG_FILE,
            'max_bytes': cls.LOG_MAX_BYTES,
            'backup_count': cls.LOG_BACKUP_COUNT
        }

class DevelopmentConfig(Config):
    """开发环境配置"""
    DEBUG = True
    TESTING = False

class ProductionConfig(Config):
    """生产环境配置"""
    DEBUG = False
    TESTING = False
    
    # 生产环境特定配置
    LOG_LEVEL = 'WARNING'
    CRAWLER_TIMEOUT = 30

class TestingConfig(Config):
    """测试环境配置"""
    DEBUG = True
    TESTING = True
    
    # 测试环境使用SQLite
    SQLALCHEMY_DATABASE_URI = 'sqlite:///:memory:'

# 配置映射
config_map = {
    'development': DevelopmentConfig,
    'production': ProductionConfig,
    'testing': TestingConfig,
    'default': DevelopmentConfig
}

def get_config(config_name: str = None) -> Config:
    """获取配置类"""
    if config_name is None:
        config_name = os.getenv('FLASK_ENV', 'default')
    
    return config_map.get(config_name, config_map['default'])
